package com.dgut.uhrs.service;

import com.dgut.uhrs.bean.Key;
import com.dgut.uhrs.bean.User;
import com.dgut.uhrs.mapper.UserMapper;
import com.dgut.uhrs.util.JWTUtil;
import com.dgut.uhrs.vo.DataVO;
import com.dgut.uhrs.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    @Transactional
    public void addUser(User user){
        userMapper.addUser(user);
    }

    public User userLogin(User user){
        return userMapper.userLogin(user);
    }

    public DataVO<UserVO> findNotClockPerson(Key key) {
        DataVO<UserVO> dataVO = new DataVO<>();
        ArrayList<UserVO> userVOS = new ArrayList<>();
        List<User> notClickPerson = userMapper.findNotClockPerson(key);
        dataVO.setCount(notClickPerson.toArray().length);
        dataVO.setCode("0");
        dataVO.setMsg("");
        for (User clickPerson : notClickPerson) {
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(clickPerson,userVO);
            userVOS.add(userVO);
        }
        dataVO.setData(userVOS);
        return dataVO;
    }


    public Boolean checkToken(String token){
        return JWTUtil.userCheckToken(token);
    }

    /**
     * 每天凌晨0点执行计划任务，更新user的打卡状态为未打卡
     *
     */
    @Scheduled(cron = "0 0 0 * * *")   //声明这是一个计划任务。
    public void updateAllIsNotClockStaus(){
        userMapper.updateAllIsNotClockStaus();
    }

    @Transactional
    public void editUser(User user) {
        userMapper.editUser(user);
    }

    public DataVO<UserVO> showAllUser(Key key) {
        DataVO<UserVO> dataVO = new DataVO<>();
        ArrayList<UserVO> userVOS = new ArrayList<>();
        List<User> users = userMapper.showAllUser(key);
        dataVO.setCount(users.toArray().length);
        dataVO.setCode("0");
        dataVO.setMsg("");
        for (User clickPerson : users) {
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(clickPerson,userVO);
            userVOS.add(userVO);
        }
        dataVO.setData(userVOS);
        return dataVO;
    }

    public void deleteUser(String userId) {
        userMapper.deleteUser(userId);
    }

    public User userCheck(User user) {
        return userMapper.userCheck(user);
    }

    public void lockWechat(User user) {
        userMapper.lockWechat(user);

    }

    public User userLoginByOpenId(String openId) {
        return userMapper.userLoginByOpenId(openId);
    }

    public User checkOpenId(String openId) {
        return userMapper.checkOpenId(openId);
    }

    public void lockUser(String userId) {
        userMapper.lockUser(userId);
    }

    public void unLockUser(String userId) {
        userMapper.unLockUser(userId);
    }
}
